﻿(function () {

    layui.use(["layer", "jquery", "form", "laydate"], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var laydate = layui.laydate;
        var buyerUserId = 0;
        var sellerUserId = 0;
        var orderPremiumTable = [];

        $(document).ready(function () {
            $("#btnSearchSeller").click(onSearchSellerBtnClick);
            $("#btnSearchBuyer").click(onSearchBuyerBtnClick);
            $("#SellerList li").click(onSellerListLiClick);
            $("#BuyerList li").click(onBuyerListLiClick);
            $("#btnCreateOrderOK").click(onCreateOrderBtnClick);
            $("#btnSetOrderPremiumTable").click(onSetOrderPremiumTableBtnClick);
            $("#btnQueryOrderPremiumTable").click(onViewOrderPremiumTableBtnClick);
        });

        function onSearchSellerBtnClick() {
            var val = $("#txtSearchSeller").val();
            var els = $("#SellerList li");
            for (var i = 0; i < els.length; i++) {
                var item = els.eq(i);
                if (item.text().indexOf(val) > -1) {
                    item.show();
                } else {
                    item.hide();
                }
            }
        }

        function onSearchBuyerBtnClick() {
            var val = $("#txtSearchBuyer").val();
            var els = $("#BuyerList li");
            for (var i = 0; i < els.length; i++) {
                var item = els.eq(i);
                if (item.text().indexOf(val) > -1) {
                    item.show();
                } else {
                    item.hide();
                }
            }
        }

        function onSellerListLiClick() {
            var el = $(this);
            var text = el.text() + "(" + el.attr("tagPhone") + ")";
            var userId = parseInt(el.attr("tagUserId"), 10);
            $("#lbSeller").text(text);
            sellerUserId = userId;
            $("#txtAmount").val("");
            for (var i = 0; i < orderPremiumTable.length; i++) {
                var item = orderPremiumTable[i];
                if (item["UserId"] == userId) {
                    $("#txtAmount").val(item["Amount"]);
                    break;
                }
            }
        }

        function onBuyerListLiClick() {
            var el = $(this);
            var text = el.text() + "(" + el.attr("tagPhone") + ")";
            $("#lbBuyer").text(text);
            buyerUserId = parseInt(el.attr("tagUserId"), 10);
        }

        function validate() {
            if (buyerUserId == 0) {
                layer.msg("请选择买家！");
            } else if (sellerUserId == 0) {
                layer.msg("请选择卖家！");
            } else if (buyerUserId == sellerUserId) {
                layer.msg("买家和买家不可以相同！");
            } else {
                var amount = parseFloat($("#txtAmount").val(), 10);
                if (amount >= 1000 && amount <= 15000) {
                    return true;
                } else {
                    layer.msg("订单金额输入有误！");
                }
            }

            return false;
        }

        function submit() {
            if (validate()) {
                var loadingIndex = layer.msg(addStyle("正在处理 ......"),
                    { icon: 16, shade: 0.01, offset: ["25%"], time: false });
                $.ajax("/ShopManage/Order/CreateOrder", {
                    method: "POST",
                    data: {
                        BuyerUserId: buyerUserId,
                        SellerUserId: sellerUserId,
                        Amount: $("#txtAmount").val()
                    },
                    complete: function (jqXHR, textStatus) {
                        layer.close(loadingIndex);
                    },
                    success: function (data, textStatus, jqXHR) {
                        $("#txtSearchSeller").focus();
                        if (data.Success == true) {
                            layer.msg(addStyle("创建订单成功！"));
                            buyerUserId = sellerUserId;
                            sellerUserId = 0;
                            $("#lbBuyer").text($("#lbSeller").text());
                            $("#lbSeller").text("请选择！");
                            $("#txtAmount").val("");
                        } else {
                            layer.msg(addStyle("创建订单失败: " + data.Msg));
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.msg(addStyle("系统错误，请联系管理员！"),
                            { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                    }
                });
            }
        }

        function onCreateOrderBtnClick() {
            submit();
        }

        function onSetOrderPremiumTableBtnClick() {
            var layId = layer.open({
                type: 1,
                title: "满堂创品",
                shade: [0.1, '#000'],
                skin: 'layui-layer-molv',
                resize: false,
                content: '<div id="SetOrderPremiumTableDlg2">' + $("#SetOrderPremiumTableDlg").html() + '</div>'
            });

            var currentDate = moment().format("YYYY-MM-DD");
            if (moment().get("hours") < 15) {
                currentDate = moment().add(-1, "days").format("YYYY-MM-DD");
            }
            laydate.render({
                elem: '#Txt-SetOrderPremiumTableDlg-Date',
                value: currentDate,
                isInitValue: true,
                showBottom: false
            });

            $("#btn-SetOrderPremiumTableDlg-OK").click(onSetOrderPremiumTableDlgOKBtnClick);

            function onSetOrderPremiumTableDlgOKBtnClick() {
                var loadingIndex = layer.msg(addStyle("正在处理 ......"),
                    { icon: 16, shade: 0.01, offset: ["25%"], time: false });
                $.ajax("/ShopManage/Order/QueryPremiumOrder", {
                    method: "POST",
                    data: {
                        date: $("#Txt-SetOrderPremiumTableDlg-Date").val()
                    },
                    complete: function (jqXHR, textStatus) {
                        layer.close(loadingIndex);
                    },
                    success: function (data, textStatus, jqXHR) {
                        if (data.Success == true) {
                            var data2 = data.Data;
                            orderPremiumTable = data2;
                            if ($.isArray(data2)) {
                                layer.close(layId);
                                orderPremiumTable = data2;
                                showOrderPremiumTableDlg(data2);
                            }
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.msg(addStyle("系统错误，请联系管理员！"),
                            { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                    }
                });
            }
        }

        function showOrderPremiumTableDlg(data) {
            layer.open({
                type: 1,
                title: "满堂创品",
                shade: [0.1, '#000'],
                skin: 'layui-layer-molv',
                resize: false,
                area: ["90%", "80%"],
                content: '<div id="ViewOrderPremiumTableDlg2">' + $("#ViewOrderPremiumTableDlg").html() + '</div>'
                , btn: ["确 定"]
            });

            var html = '';
            data.forEach(item => {
                html += '<li>' + item["Name"] + ': ' + item["Amount"] + '</li>';
            });
            $("#ViewOrderPremiumTableDlg-List").append(html);
        }

        function onViewOrderPremiumTableBtnClick() {
            showOrderPremiumTableDlg(orderPremiumTable);
        }

        function addStyle(str) {
            return '<span style="font-size: 16px;">' + str + '</span>';
        }
    });

})();